<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  lineData: {
    type: Object,
    default: () => {
    }
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    const date = new Date();
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    myChart.value = echarts.init(echartDom.value);
    let tempSeries = [
      {
        data: props?.lineData?.seriesData[0],
        name: '实际走势',
        type: 'line',
        smooth: false,
        showSymbol: false,
        itemStyle: {
          color: '#0E81FF'
        }
      },
      {
        data: props?.lineData?.seriesData[1],
        name: '时间序列预测',
        type: 'line',
        smooth: false,
        showSymbol: false,
        itemStyle: {
          color: '#F8F364'
        },
        markArea: {
          silent: true,
          itemStyle: {
            color: '#0E81FF29',
          },
          label: {
            color: 'white',
            fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes)
          },
          data: [
            [
              {
                xAxis: props?.lineData?.forecastIndex
              },
              {
                xAxis: props?.lineData?.xAxisData?.length - 1
              }
            ]
          ]
        }
      },
    ]
    if (props?.lineData?.seriesData?.length === 3) {
      tempSeries.push({
        data: props?.lineData?.seriesData[2],
        name: '预期变动',
        type: 'line',
        smooth: false,
        showSymbol: false,
        itemStyle: {
          color: '#5ABE52'
        },
        markArea: {
          silent: true,
          itemStyle: {
            color: '#0E81FF29',
          },
          label: {
            color: 'white',
            fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes)
          },
          data: [
            [
              {
                xAxis: props?.lineData?.forecastIndex
              },
              {
                xAxis: props?.lineData?.xAxisData?.length - 1
              }
            ]
          ]
        }
      })
    }
    let option = {
      tooltip: {
        trigger: 'axis',
        formatter: (val) => {
          const tempArr = val?.filter(x => x.value !== undefined)
          let tempDom = ''
          tempArr.forEach((x, i) => {
            tempDom += `${tempArr[i].seriesName}： ${tempArr[i].value}${props?.lineData?.yAxisName}` + '<br>'
          })
          return (
            `${val[0].name}` + '<br>' + tempDom
          )
        }
      },
      grid: {
        left: 0,
        right: '6%',
        bottom: 0,
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 25 : 15}%`,
        containLabel: true
      },
      legend: {
        x: props?.dialogData?.fontSizeTimes === 1 && props?.lineData?.legendData?.length === 3 ? 'right' : 'center',
        icon: 'rect',
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 7 : 0}%`,
        itemWidth: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        itemHeight: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        textStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          padding: [pxToRem(2), 0, 0],
          rich: {}
        },
        tooltip: {
          show: true
        },
        formatter: (params) => {
          let val = '';
          if (params.length > 6) {
            val = `${params.substr(0, 5)}...`;
            return val;
          } else {
            return params;
          }
        },
        data: props?.lineData?.legendData
      },
      xAxis: {
        min: 0,
        type: 'category',
        boundaryGap: false,
        data: props?.lineData?.xAxisData,
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter',
          showMinLabel: false,
          interval: props?.dialogData?.fontSizeTimes === 1 ? 'auto' : 0,
        },
        axisLine: {
          show: true // 不显示坐标轴线
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
      },
      yAxis: {
        max: (value) => value.max || 3000,
        name: props?.lineData?.yAxisName,
        nameTextStyle: {
          color: "white",
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          fontWeight: 'lighter',
          padding: [0, 0, 0, pxToRem(-10 * props?.dialogData?.fontSizeTimes)]
        },
        nameGap: pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 15 : 25),
        textStyle: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes)
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: true, // 不显示坐标轴线
        },
        splitLine: {
          show: false,
        },
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter'
        }
      },
      dataZoom: { // 数据过多拖动逻辑
        type: "slider",
        startValue: 0,
        height: pxToRem(20),
        bottom: 0
      },
      series: tempSeries
    }
    option && myChart.value.setOption(option, true);

    myChart.value.on('legendselectChanged', (params) => {
      event.stopPropagation();
    })
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};


defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h120 {
  height: 120px;
}
.dialog-h120 {
  width: 1120px;
  height: 500px;
}
</style>